<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace api\doctor\controller;

use think\Db;
use cmf\controller\RestBaseController;
use think\Config;
use think\Cache;
use \Qcloud\sms\SmsSingleSender AS sms;

class HelperController extends RestBaseController
{

    
    public $data;
    public $bizid;
    public $type;
    public $tel;
    public $limit = 10;
    public $doc_id;
    public $doc_ids;

    public function __construct(){
        $token =  input('token') ? input('token') : $_SERVER['HTTP_TOKEN'];
        $appid =  input('appid') ? input('appid') : $_SERVER['HTTP_APPID'];
        if(!$token){
            $this->error('缺少:token');
        }
        if(!Cache::get($token)){
            echo json_encode(array('code'=>90001,'msg'=>'token已过期'));die;
        }
        preg_match_all('/\d+/',$token,$arr);
        if(count(@$arr[0]) != 3){
            $this->error('token:有误!');
        }
        if($arr[0][2] == 2){//如果是医生
                $bizid = Db::prefix_close()->table('doctor')
                    ->where(['tel'=>$arr[0][1]])
                    ->field('biz_id,id')
                    ->find();
            $doc_id = $bizid['id'];
            $bizid = $bizid['biz_id'];
            $this->type= 2;
        }else{//如果是助手 就只返回他的医生的数据
            $hep = Db::prefix_close()->table('helper')
                    ->where(['tel'=>$arr[0][1]])
                    ->field('biz_id,doc_id')
                    ->find();
            $bizid = $hep['biz_id'];
            $doc_id = $hep['doc_id'];
            $this->type = 3;
        }
        $this->doc_id = isset($doc_id) ? 'c.id = '.$doc_id : '';
        $this->doc_ids = isset($doc_id) ? $doc_id : '';
        $this->bizid = $bizid;
        $this->data = input();
    }
   
    /**
     * [index 我的助手列表]
     * @return [type] [description]
     */
    public function index(){
        if(!isset($this->data['page'])||empty($this->data['page'])||$this->data['page']<=0){
            $this->data['page'] = 1;
        }
        if (!isset($this->data['limit'])||empty($this->data['limit'])) {
            $this->data['limit'] = $this->limit;
        }
        $limit = $this->data['limit'];
        $pages = ($this->data['page']-1)*$limit;
        $data = Db::table('helper')
            ->field('id,name,tel')
            ->where(['biz_id'=>$this->bizid,'doc_id'=>$this->doc_ids])
            ->limit($pages,$limit)
            ->order('addtime desc')
            ->select();
        $newdata = [];
        foreach ($data as $key => $value) {
            $newdata[$key]['id'] = $value['id'];
            $newdata[$key]['name'] = $value['name'];
            $newdata[$key]['tel'] = $value['tel'];
        }
        $this->success('请求成功！',$newdata);
    }

    /**
     * [helperAdd 助手添加]
     * @return [type] [description]
     */
    public function helperAdd(){
        if (!isset($this->data['name'])||empty($this->data['name'])) {
            $this->error('请填写助手姓名!');
        }
        if (!isset($this->data['tel'])||empty($this->data['tel'])) {
            $this->error('请填写助手电话号码!');
        }
        //检测此号码是否为医生
        if(Db::table('doctor')->where(['tel'=>$this->data['tel']])->find()){
            $this->error('不能添加医生为助手!');
        }
        if(Db::table('helper')->where(['tel'=>$this->data['tel']])->find()){
            $this->error('此助手已存在!');
        }
        $rq = Db::table('helper')->insert([
            'biz_id'=>$this->bizid,
            'doc_id'=>$this->doc_ids,
            'name'=>$this->data['name'],
            'tel'=>$this->data['tel'],
            'addtime'=>date('Y-m-d H:i:s')
        ]);
        if($rq){
            $this->success('添加成功!');
        }
        $this->error('添加失败!');
    }

    /**
     * [helperdle 助手删除]
     * @return [type] [description]
     */
    public function helperdle(){
        if (!isset($this->data['tel'])||empty($this->data['tel'])) {
            $this->error('缺少助手:id');
        }
        $rq = Db::table('helper')->where(['tel'=>$this->data['tel']])->delete();
        if($rq){
            $this->success('删除成功!'); 
        }
        $this->error('删除失败!');
    }
}